class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
    	vector<int> record;
		vector<vector<int> > result;

		vector<int> num = candidates;
		sort(num.begin(), num.end());

		int size = unique(num.begin(), num.end()) - num.begin();
		for (int i = num.size() - 1; i >= size; --i) {
			num.pop_back();
		}
		CalculateSum(num, target, 0, 0, record, result);
		return result;
    }

	void CalculateSum(const vector<int> &num, int target, int start, int sum, vector<int> &record, vector<vector<int> >& result) {
		for (int i = start; i < num.size(); ++i) {
			if (num[i] + sum > target) {
				break;
			}
			record.push_back(num[i]);
			if (num[i] + sum == target) {
				result.push_back(record);
			}
			CalculateSum(num, target, i, sum + num[i], record, result);
			record.pop_back();
		}
	}

};